<%@ include file="Common.jsp" %>
<%@ page import="de.localsport.util.MatchResultUpdator" %>

<%!
//
//   Filename: MatchResult.jsp
//   Generated with CodeCharge  v.1.2.0
//   JSP.ccp build 05/21/01
//

static final String sFileName = "MatchResult.jsp";
              
%><%

String cSec = checkSecurity(1, session, response, request);
if ("sendRedirect".equals(cSec) ) return;
                
boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sMemberId = (String) session.getAttribute("UserID");
String sMatchesErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;

String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}
if ( sForm.equals("Matches") ) {
  sMatchesErr = MatchesAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(sMatchesErr)) return;
}

%>            
<html>
<head>
<title>Soccer Zocker - Place Bet</title>

</head>
<body style="background-color: #006600; color: #000000; font-family: Arial, Tahoma, Verdana, Helveticabackground-color: #FFFFFF; color: #000000; font-family: Arial, Tahoma, Verdana, Helvetica">
<jsp:include page="Header.jsp" flush="true"/>
 <table>
  <tr>   
   <td valign="top"><center>
<% Matches_Show(request, response, session, out, sMatchesErr, sForm, sAction, conn, stat); %>
    <!--<SCRIPT Language="JavaScript">
if (document.forms["Matches"])
document.Matches.onsubmit=delconf;
function delconf() {
if (document.Matches.FormAction.value == 'delete')
  return confirm('Delete record?');
}
</SCRIPT>-->
   </center></td>
  </tr>
 </table>

<jsp:include page="Footer.jsp" flush="true"/>
</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!


  String MatchesAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
  
    String sMatchesErr ="";
    try {

      if (sAction.equals("")) return "";
      String sSQL="";
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "AdminMatchResults.jsp?type=0";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;

      String pPKmatch_id = "";
      
      if (sAction.equalsIgnoreCase("cancel") ) {
        try {
          if ( stat != null ) stat.close();
          if ( conn != null ) conn.close();
        }
        catch ( java.sql.SQLException ignore ) {}
        response.sendRedirect (sActionFileName);
        return "sendRedirect";
      }

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }

      // Create WHERE statement

      if ( iAction == iupdateAction || iAction == ideleteAction ) { 
        pPKmatch_id = getParam(request, "match_id");
        if (isEmpty(pPKmatch_id)) return sMatchesErr;       
        sWhere = "match_id=" + toSQL(pPKmatch_id, adNumber);
      }

      String fldmember_id="";
      String fldmatch_id="";
      String fldhome_score="";
      String fldguest_score="";
      String fldguest="";
      String fldhome="";
      String fldbet_tip="";
      String fldscore="";
      String fldtimeout="";
      String fldgroup="";

      // Load all form fields into variables
    
      fldmember_id = getParam(request, "member_id");
      fldmatch_id = getParam(request, "match_id");
      fldhome_score = getParam(request, "home_score");
      fldguest_score = getParam(request, "guest_score");
      fldguest = getParam(request, "guest");
      fldhome = getParam(request, "home");
            
      // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if ( isEmpty(fldhome_score) ) {
          sMatchesErr = sMatchesErr + "Eingabe im Feld \"Tipp H\" wird benoetigt.<br>";
        }
        if ( isEmpty(fldguest_score) ) {
          sMatchesErr = sMatchesErr + "Eingabe im Feld \"Tipp G\" wird benoetigt.<br>";
        }
        if ( ! isNumber(fldhome_score)) {
          sMatchesErr = sMatchesErr + "Der Wert im Feld \"Tipp H\" ist nicht erlaubt.<br>";
        }
        if ( ! isNumber(fldguest_score)) {
          sMatchesErr = sMatchesErr + "Der Wert im Feld \"Tipp G\" ist nicht erlaubt.<br>";
        }
        
        /*
        if ( ! isEmpty(fldmember_login)) {
          iCount = 0;
          if ( iAction == iinsertAction ) {
            iCount = dCountRec(stat, "Matches", "member_login=" + toSQL(fldmember_login, adText));
          }
          else {
            if ( iAction == iupdateAction ) {
              iCount = dCountRec( stat, "Matches", "member_login=" + toSQL(fldmember_login, adText) + " and not(" + sWhere + ")");
            }
          }
          if (iCount > 0) {
            sMatchesErr = sMatchesErr + "The value in field Login is already in database.<br>";
          }
        }
        */
        
        if (sMatchesErr.length() > 0 ) {
          return (sMatchesErr);
        }
      }


      sSQL = "";
      // Create SQL statement

      switch (iAction) {
        case iinsertAction :
            sSQL = "insert into matches (match_id, member_id, home_score, guest_score)" +
                   " values (" + 
                	toSQL(fldmatch_id, adNumber) + "," +
                        toSQL(fldmember_id, adNumber) + "," +
                        toSQL(fldhome_score, adNumber) + "," +
                        toSQL(fldguest_score, adNumber) + ")";
            break;
  
      	case iupdateAction:
/*
            sSQL = "update matches set " +
                "home_score=" + toSQL(fldhome_score, adNumber) + ", " +
                "guest_score=" + toSQL(fldguest_score, adNumber);
            sSQL = sSQL + " where " + sWhere;
*/
            break;
      
      	case ideleteAction:
           sSQL = "delete from Matches where " + sWhere;
           break;
      }

      if ( sMatchesErr.length() > 0 ) return sMatchesErr;
      try {
        if (iAction == iupdateAction) {
            MatchResultUpdator u = new MatchResultUpdator(conn);
            u.updateMatchResult(toSQL(fldmatch_id, adNumber), toSQL(fldhome_score, adNumber), toSQL(fldguest_score, adNumber));
        } else {
	    // Execute SQL statement
	    stat.executeUpdate(sSQL);
        }
      }
      catch(java.sql.SQLException e) {
        sMatchesErr = e.toString(); return (sMatchesErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      response.sendRedirect (sActionFileName);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (sMatchesErr);
  }


  void Matches_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sMatchesErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      java.sql.Statement tmpStat = null;
      
      String pmatch_id = "";

      String fldmember_id="";
      String fldmatch_id="";
      String fldhome_score="";
      String fldguest_score="";
      String fldguest="";
      String fldhome="";
      String fldguest_id="";
      String fldhome_id="";
      String fldbet_tip="";
      String fldscore="";
      String fldtimeout="";
      String fldgroup="";

      boolean bPK = true;

      if ( "".equals(sMatchesErr)) {
        // Load primary key and form parameters        
        fldmatch_id = getParam( request, "match_id");
        pmatch_id = getParam( request, "match_id");
        fldmatch_id = getParam(request, "match_id");
        fldhome_score = getParam(request, "home_score");
        fldguest_score = getParam(request, "guest_score");
      }
      else {
        // Load primary key, form parameters and form fields
        fldmember_id = getParam( request, "member_id");
        fldmatch_id = getParam( request, "match_id");
        fldmatch_id = getParam( request, "id");
        fldhome_score = getParam(request, "home_score");
        fldguest_score = getParam(request, "guest_score");
        fldscore = getParam(request, "score");
        fldhome = getParam(request, "home");
        fldguest = getParam(request, "guest");
        fldtimeout = getParam(request, "timeout");
        fldgroup = getParam(request, "tgroup");
        
        pmatch_id = getParam( request, "match_id");
      }
	   
      if ( isEmpty(pmatch_id)) { bPK = false; }      
      
       sWhere += "match_id=" + toSQL(pmatch_id, adNumber);    
	  sSQL = "select * from matches where " + sWhere;

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "Matches".equals(sForm))) {
        
        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
              
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        
        if ( "".equals(sMatchesErr)) {                
          fldmatch_id = (String) rsHash.get("match_id");
          fldhome_score = (String) rsHash.get("home_score");
          fldguest_score = (String) rsHash.get("guest_score");
          fldhome_id = (String) dLookUp(tmpStat, "matches", "home", "match_id=" + fldmatch_id);
          fldguest_id = (String) dLookUp(tmpStat, "matches", "guest", "match_id=" + fldmatch_id);          
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
          fldgroup = (String) dLookUp(tmpStat, "matches", "tgroup", "match_id=" + fldmatch_id);          
          fldhome = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldhome_id);
          fldguest = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldguest_id);
        }

        if (sAction.equals("") || ! "Matches".equals(sForm)) {
          fldmatch_id = (String) rsHash.get("match_id");         
          fldhome_score = (String) rsHash.get("home_score");
          fldguest_score = (String) rsHash.get("guest_score");
          fldhome_id = (String) dLookUp(tmpStat, "matches", "home", "match_id=" + fldmatch_id);
          fldguest_id = (String) dLookUp(tmpStat, "matches", "guest", "match_id=" + fldmatch_id);          
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
          fldgroup = (String) dLookUp(tmpStat, "matches", "tgroup", "match_id=" + fldmatch_id);          
          fldhome = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldhome_id);
          fldguest = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldguest_id);
        }
      }
      else {
        
        if ( "".equals(sMatchesErr)) {
          fldmatch_id = toHTML(getParam(request,"match_id"));
          fldhome_id = (String) dLookUp(tmpStat, "matches", "home", "match_id=" + fldmatch_id);
          fldguest_id = (String) dLookUp(tmpStat, "matches", "guest", "match_id=" + fldmatch_id);          
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
          fldgroup = (String) dLookUp(tmpStat, "matches", "tgroup", "match_id=" + fldmatch_id);          
          fldhome = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldhome_id);
          fldguest = (String) dLookUp(tmpStat, "teams", "name", "team_id=" + fldguest_id);
        }
      }

      out.println("<center>");
      out.println("&nbsp;<br><br>");     
      out.println("<table bgcolor=\"#000000\" cellspacing=\"2\" cellpadding=\"3\">");
      out.println("<tr>\n"+
      		"<td style=\"background-color: #FFCC66\" colspan=\"6\"><center><font style=\"font-size: 14pt; font-weight: bold\">Ergebnis f&uuml;r Spiel Nummer " + toHTML((String) getParam(request, "match_id")) + "</font></center></td>\n</tr>");
      if ( ! sMatchesErr.equals("")) {
        out.println("<tr>\n<td style=\"background-color: #006600\" colspan=\"6\"><font style=\"font-size: 10pt; color: #ffffff\">"+sMatchesErr+"</font></td>\n</tr>");
      }
      sMatchesErr="";
      out.println("<form method=\"get\" action=\""+sFileName+"\" name=\"Matches\">");

      // Show form field
      out.print("<tr>\n");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Anstoss</font></td>");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Gruppe</font></td>");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Heim</font></td>");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Gast</font></td>");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Ergebnis</font></td>");
      out.println("\n</tr>");
            
      out.print("<tr>\n");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + toHTML(fldtimeout) + "</font></td>");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + toHTML(fldgroup) + "</font></td>");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + toHTML(fldhome) + " <img src=\"images/" + fldhome + ".gif\"/></font></td>");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + "<img src=\"images/" + fldguest + ".gif\"/> " + fldguest + "</font></td>");
      out.print("<td style=\"background-color: #006600\" align=\"center\">");
      out.print("<input type=\"text\" name=\"home_score\" maxlength=\"2\" value=\"" + toHTML(fldhome_score) + "\" size=\"2\">");
      out.print("<input type=\"text\" name=\"guest_score\" maxlength=\"2\" value=\"" + toHTML(fldguest_score) + "\" size=\"2\">");
      out.print("</td>");
      out.println("\n</tr>");
      out.print("<tr>\n<td bgcolor=\"ffffff\" colspan=\"6\" align=\"right\">");

      if ( bPK && ! (sAction.equals("insert") && "Matches".equals(sForm))) {
        out.print("<input type=\"submit\" value=\"Update\" onclick=\"document.Matches.FormAction.value = 'update';\"> ");        
        out.print("<input type=\"submit\" value=\"Cancel\" onclick=\"document.Matches.FormAction.value = 'cancel';\">");
        //out.print("<input type=\"submit\" value=\"Delete\" onclick=\"document.Matches.FormAction.value = 'delete';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Matches\">");
        out.print("<input type=\"hidden\" value=\"update\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input type=\"submit\" value=\"Update\" onclick=\"document.Matches.FormAction.value = 'update';\">");
        out.print("<input type=\"submit\" value=\"Cancel\" onclick=\"document.Matches.FormAction.value = 'cancel';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Matches\">");
        out.print("<input type=\"hidden\" value=\"update\" name=\"FormAction\">");
      }
      
      out.print("<input type=\"hidden\" name=\"home\" value=\""+toHTML(fldhome)+"\">");
      out.print("<input type=\"hidden\" name=\"guest\" value=\""+toHTML(fldguest)+"\">");
      out.print("<input type=\"hidden\" name=\"timeout\" value=\""+toHTML(fldtimeout)+"\">");
      out.print("<input type=\"hidden\" name=\"group\" value=\""+toHTML(fldgroup)+"\">");
      out.print("<input type=\"hidden\" name=\"match_id\" value=\""+toHTML(fldmatch_id)+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n</tr>\n</form>\n</table></center>&nbsp;<br><br>");

    }
    catch (Exception e) { out.println(e.toString()); }
  }
  
  %>
  
  
  
  